<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">总公司名称</label>
        <el-input
          v-model="query.companyName"
          clearable
          placeholder="总公司名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">手机号</label>
        <el-input
          v-model="query.phone"
          clearable
          placeholder="手机号"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="150px"
        >
          <!-- <el-form-item label="总公司主键id" prop="companyId">
            <el-input v-model="form.companyId" style="width: 370px" />
          </el-form-item> -->
          <el-form-item label="总公司名称">
            <el-input v-model="form.companyName" style="width: 370px" />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.phone" style="width: 370px" />
          </el-form-item>
          <el-form-item label="总公司品牌logo">
            <el-upload
              :action="imgUploadApi"
              :file-list="fileList"
              :limit="3"
              v-model="form.companyBrandLogoUrl"
              :on-success="ImgSuccess"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary"
                >点击上传品牌馆图片 <i class="el-icon-upload el-icon--right"></i
              ></el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>
          <!--
          <el-form-item label="总公司首页轮播图">
            <el-upload
              :action="bannerUploadApi"
              :file-list="fileList"
              :limit="3"
              v-model="form.companyBannerUrl"
              :on-success="BannerSuccess"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary"
                >点击上传首页轮播图 <i class="el-icon-upload el-icon--right"></i
              ></el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>
          -->
          <el-form-item label="总公司首页展示视频">
            <el-upload
              :action="videoUploadApi"
              :file-list="fileList"
              :limit="3"
              v-model="form.bannerUrl"
              :on-success="VideoSuccess"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary"
                >点击上传首页的视频 <i class="el-icon-upload el-icon--right"></i
              ></el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传mp4文件，且不超过10MB
              </div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="companyName" label="总公司名称" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column label="总公司品牌logo">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="imgUrl + scope.row.companyBrandLogoUrl"
            ></el-image>
          </template>
        </el-table-column>

        <!--
        <el-table-column label="首页轮播图">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="imgUrl + scope.row.companyBannerUrl"
            ></el-image>
          </template>
        </el-table-column>
        -->
        <el-table-column prop="companyVideoUrl" label="总公司首页展示视频" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />

        <el-table-column
          v-if="checkPer(['admin', 'company:edit', 'company:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCompany from "@/api/company/company.js";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";

const defaultForm = {
  companyId: null,
  companyName: null,
  companyBannerUrl: null,
  companyBrandLogoUrl: null,
  companyVideoUrl: null,
  createTime: null,
  updateTime: null,
  phone: null,
};
export default {
  name: "Company",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["company_id"],
  cruds() {
    return CRUD({
      title: "CompanyController",
      url: "api/company",
      idField: "companyId",
      sort: "companyId,desc",
      crudMethod: { ...crudCompany },
    });
  },
  data() {
    return {
      fileList: [],
      imgUrl: "https://www.f-cloud.top/web/file/图片/",
      bannerUploadApi: "https://www.f-cloud.top/web/api/company/uploadBanner",
      imgUploadApi: "https://www.f-cloud.top/web/api/company/uploadImg",
      videoUploadApi: "https://www.f-cloud.top/web/api/company/uploadVideo",
      permission: {
        add: ["admin", "company:add"],
        edit: ["admin", "company:edit"],
        del: ["admin", "company:del"],
      },
      rules: {
        companyId: [
          { required: true, message: "总公司主键id不能为空", trigger: "blur" },
        ],
      },
      queryTypeOptions: [
        { key: "companyName", display_name: "总公司名称" },
        { key: "phone", display_name: "手机号" },
      ],
    };
  },
  methods: {
    BannerSuccess(res, file) {
      this.form.companyBannerUrl = res;
    },
    ImgSuccess(res, file) {
      this.form.companyBrandLogoUrl = res;
    },
    VideoSuccess(res, file) {
      this.form.companyVideoUrl = res;
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件`);
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped>
/* h1 {
    font-size: small;
    font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  } */
</style>
